/***************************************************
	FileName: sshell.c
	Purpose: Execute all unix shell commands through the executable file.
	Description: Ties in all the individual pieces of the shell utility
        that we are creating.
	Modified by Grant Meyers
***************************************************/
#include "parser.h"
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>

int main(void) {
  char input[MAXINPUTLINE];
  
  signal_c_init();

  if(printf("Welcome to the sample shell!  You may enter commands here, one\n")<0)  {
    perror("printf error.  sshell.c line 19");
    exit(EXIT_FAILURE);
  }
  if(printf("per line.  When you're finished, press Ctrl+D on a line by\n")<0)  {
    perror("printf error.  sshell.c line 23");
    exit(EXIT_FAILURE);
  }
  if(printf("itself.  I understand basic commands and arguments separated by\n")<0)  {
    perror("printf error.  sshell.c line 27");
    exit(EXIT_FAILURE);
  }
  if(printf("spaces, redirection with < and >, up to two commands joined\n")<0)  {
    perror("printf error.  sshell.c line 31");
    exit(EXIT_FAILURE);
  }
  if(printf("by a pipe, tilde expansion, and background commands with &.\n\n")<0)  {
    perror("printf error.  sshell.c line 35");
    exit(EXIT_FAILURE);
  }
  
  if(printf("\nGMsh$ ")<0)  {
    perror("printf error.  sshell.c line 17");
    exit(EXIT_FAILURE);
  }

  char* fgets_ret = 0;

  while ((fgets_ret = fgets(input, sizeof(input), stdin))) {
    if(fgets_ret == NULL) {
      perror("Error fgets returned null.  sshell.c line 48");
      exit(EXIT_FAILURE);
    }
    stripcrlf(input);
    parse(input);
    if(printf("\nGMsh$ ")<0)  {
      perror("printf error.  sshell.c line 54");
      exit(EXIT_FAILURE);
    }
  }
  return 0;
}
